package com.hornsun.admin;

import com.hornsun.util.StrUtil;
import com.hornsun.domain.dbo.MemberWechat;
import com.hornsun.domain.dbo.UserInfo;
import com.hornsun.repository.MemberWechatRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by 编译中... on 2018/5/26.
 */
@RestController
@RequestMapping("/api/account")
public class AccountAPI {

    @Resource
    private MemberWechatRepository memberWechatRepository;

    @PostMapping("/updateUserInfo")
    public ResponseEntity<?> updateUserInfo(HttpServletRequest httpRequest,UserInfo userInfo){
        String openid = httpRequest.getAttribute("openid").toString();
        if (openid.isEmpty()){
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        MemberWechat memberWechat = new MemberWechat();
        memberWechat.setLastLoginTime(new Date());
        memberWechat.setOpenid(openid);
        memberWechat.setCity(userInfo.getCity());
        memberWechat.setGender(userInfo.getGender());
        memberWechat.setHead(userInfo.getAvatarUrl());
        memberWechat.setNickname(userInfo.getNickName());
        memberWechat.setTerminal(StrUtil.toSystem(userInfo.getSystem()));
        memberWechatRepository.updateMemberWechat(memberWechat);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    @GetMapping("/getOpenid")
    public ResponseEntity<?> getOpenid(HttpServletRequest httpRequest){
        return new ResponseEntity<Object>(httpRequest.getAttribute("openid").toString(),HttpStatus.OK);
    }

}
